package com.freecat.xmqgct.app;

import android.app.Application;
import android.content.Context;
import android.support.v4.app.FragmentManager;

import com.google.gson.GsonBuilder;
import com.freecat.xmqgct.BuildConfig;
import com.freecat.xmqgct.mvp.model.api.Api;

import java.net.Proxy;
import java.util.List;
import java.util.concurrent.TimeUnit;

import me.jessyan.art.base.delegate.AppLifecycles;
import me.jessyan.art.di.module.AppModule;
import me.jessyan.art.di.module.ClientModule;
import me.jessyan.art.di.module.GlobalConfigModule;
import me.jessyan.art.http.imageloader.glide.GlideImageLoaderStrategy;
import me.jessyan.art.http.log.RequestInterceptor;
import me.jessyan.art.integration.ConfigModule;
import okhttp3.OkHttpClient;

/**
 * 作者：win
 * 时间：2019/6/26
 * 注释：
 * @author ChenPeng
 */
public class GlobalConfiguration implements ConfigModule {
    @Override
    public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
        if (!BuildConfig.DEBUG) {
            builder.printHttpLogLevel(RequestInterceptor.Level.NONE);
        }
        builder.baseurl(Api.APP_DOMAIN)
                .globalHttpHandler(new GlobalHttpHandlerImp(context))
                .imageLoaderStrategy(new GlideImageLoaderStrategy())
                .responseErrorListener(new ResponseErrorListenerImp())
                .gsonConfiguration(new AppModule.GsonConfiguration() {
                    @Override
                    public void configGson(Context context, GsonBuilder builder) {
                        builder.serializeNulls()
                                .enableComplexMapKeySerialization();
                    }
                })
                .okhttpConfiguration(new ClientModule.OkhttpConfiguration() {
                    @Override
                    public void configOkhttp(Context context, OkHttpClient.Builder builder) {
                        builder.writeTimeout(10, TimeUnit.SECONDS);
                        builder.proxy(Proxy.NO_PROXY);
                    }
                });
    }

    @Override
    public void injectAppLifecycle(Context context, List<AppLifecycles> lifecycles) {
        lifecycles.add(new AppLifecycleImp());
    }

    @Override
    public void injectActivityLifecycle(Context context, List<Application.ActivityLifecycleCallbacks> lifecycles) {
        lifecycles.add(new ActivityLifecycleImp());
    }

    @Override
    public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {

    }
}
